<?php
/**
 * 用户模块
 */

class User_Model extends CI_Model
{
    private $table = 'users';
    private $record;
    private $is_logged_in = false;
    private $is_active = false;
     
    public function __construct()
    {
        parent::__construct();

        if(isset($_SESSION[SESSION_KEY]) && isset($_SESSION[SESSION_KEY]['email']))
        {
            if(isset($_SESSION[SESSION_KEY]['record']))
                $user = $_SESSION[SESSION_KEY]['record'];
            else
                $user = $this->find_by_email($_SESSION[SESSION_KEY]['email']);
        }
        else if(isset($_COOKIE[COOKIE_KEY]))
        {
            $user = $this->check_cookie($_COOKIE[COOKIE_KEY]);
        }
        else
        {
            return false;
        }
        
        if(!$user)
            return $this->logout();

        $this->set_info($user);
        return true;
    }

    public function set_info($user)
    {
        $_SESSION[SESSION_KEY]['email'] = $user->email;
        $_SESSION[SESSION_KEY]['record'] = $user;
        $this->record = $user;
        $this->is_logged_in = true;
        $this->is_active = $user->is_active;
    }

    public function is_login()
    {
        return $this->is_logged_in;
    }

    public function is_active()
    {
        return $this->is_active;
    }

    public function get_id()
    {
        return $this->record ? $this->record->user_id : false;
    }

    public function get_name()
    {
        return $this->record ? $this->record->name : false;
    }

    public function get_userinfo()
    {
        return $this->record ? $this->record : false;
    }

    public function get_email()
    {
        return $this->record ? $this->record->email : false;
    }

    public function get_permission()
    {
        
    }

    public function logout()
    {
        if(isset($_SESSION[SESSION_KEY]))
            unset($_SESSION[SESSION_KEY]);
        $this->del_cookie();
        $this->record = false;
        $this->is_logged_in = false;
        $this->permissions = array();
        $this->is_active = false;
    }

    public function login($email=false, $passwd=false, $set_cookie=false)
    {
        if(!$email || !$passwd)
            return false;
        
        $this->logout();
        $email = strtolower($email);
        $user = $this->find_by_email($email);

        if(md5(trim($passwd)) == $user->passwd)
        {
            if($set_cookie)
            {
                $time = time() + COOKIE_LIFE;
                $value = $this->bake_user_cookie($time, $user);
                setcookie(COOKIE_KEY, $value, $time, COOKIE_PATH, COOKIE_DOMAIN);
            }
            $this->set_info($user);

            return true;
        }
        return false;
    }

    public function bake_user_cookie($time, $user)
    {
        return 'exp='.$time.'&id='.$user->user_id.'&digest='.md5($user->email.$user->passwd);
    }

    public function check_cookie($cookie)
    {
        $params = $this->explode_cookie($cookie);
        if (isset($params['exp'], $params['id'], $params['digest'])) {
            if ( !$user = $this->find_by_id($params['id']))
                return false;

            if ($this->bake_user_cookie($params['exp'], $user) == $cookie && $params['exp'] > time())
                return $user;
        }
        return false;
    }

    public function explode_cookie($cookie)
    {
        $pieces = explode('&', $cookie);
        if (count($pieces) < 2)
            return array();
        foreach ($pieces as $piece) {
            list($key, $value) = explode('=', $piece);
            $params[$key] = $value;
        }
        return $params;
    }

    public function del_cookie()
    {
        setcookie(COOKIE_KEY, false, time() - COOKIE_LIFE, COOKIE_PATH, COOKIE_DOMAIN);
    }

    public function add($userinfo=false)
    {
        if(!$userinfo)
            return false;
        
        $userinfo['email'] = strtolower($userinfo['email']);
        if(!empty($userinfo['passwd']))
            $userinfo['passwd'] = md5($userinfo['passwd']);
        
        $this->db->insert($this->table, $userinfo);
        if($this->db->affected_rows()>0)
        {
            return $this->db->insert_id();
        }
        return false;
    }

    public function update($data=false)
    {
        if(!is_array($data=false))
            return false;
        $this->update($this->table, $data, array('user_id'=>$this->get_id()));
        if($this->db->affected_rows() > 0)
            return true;
        return false;
    }

    public function reset_passwd($old_passwd=false, $new_passwd=false)
    {
        if( md5($old_passwd) != $this->record->passwd )
            return false;
        return $this->update(array('passwd'=>md5($new_passwd)), $this->get_id());
    }

    public function find_by_email($email=false)
    {
        if(!$email)
            return false;
        $query = $this->db->get_where($this->table, array('email'=>$email), $limit=1);
        if($query->num_rows()>0)
        {
            return $query->row();
        }
        return false;
    }

    public function find_by_id($id=false)
    {
        if(!$id)
            return false;
        $query = $this->db->get_where($this->table, array('user_id'=>$id), $limit=1);
        if($query->num_rows()>0)
        {
            return $query->row();
        }
        return false;
    }

}